package com.warmheart.core.pool;

import java.util.function.Consumer;
import java.util.function.Supplier;

import javax.validation.constraints.NotNull;

/**
 * 
 * @ClassName: PoolTaskUtil
 * @Description: 多线程执行工具类
 * @author ZhangJianyang
 * @date 2025-06-16 09:37:02
 *
 */
public class PoolResultFactory {

    protected PoolResultFactory() {
    }

    /**
     * 
     * @Title: custom
     * @Description: 创建Builder
     * @author ZhangJianyang
     * @return
     */
    public static <T> PoolResultFactory.SupplierBuilder<T> supplier(@NotNull Supplier<T> supplier,
            @NotNull String remarks) {
        return new SupplierBuilder<T>(supplier, remarks);
    }

    public static <T> PoolResultFactory.SupplierBuilder<T> supplier(@NotNull Supplier<T> supplier,
            @NotNull String remarks, @NotNull Consumer<T> consumer) {
        return new SupplierBuilder<T>(supplier, remarks);
    }

    /**
     * 
     * @ClassName: Builder
     * @Description: Builder模式构造类
     * @author ZhangJianyang
     * @date 2024年6月13日 下午4:02:46
     *
     */
    public static class SupplierBuilder<T> {

        private WhScfInfo<T> scInfo;

        private SupplierBuilder(Supplier<T> supplier, String remarks) {
            this.scInfo = new WhScfInfo<T>() {
                @Override
                public WhSupplier<T> supplier() {
                    return new WhSupplier<T>(remarks, supplier);
                }

                @Override
                public Consumer<T> consumer() {
                    return null;
                }
            };
        }

        /**
         * 
         * @Title: put
         * @Description: 添加包含返回值的执行逻辑
         * @author ZhangJianyang
         * @param supplierName
         *            执行逻辑名称，用于返回值取值对应的KEY
         * @param supplier
         *            执行逻辑
         * @param remarks
         *            任务备注
         * @return
         */
        private SupplierBuilder(Supplier<T> supplier, String remarks, Consumer<T> consumer) {
            this.scInfo = new WhScfInfo<T>() {

                @Override
                public WhSupplier<T> supplier() {
                    return new WhSupplier<T>(remarks, supplier);
                }

                @Override
                public Consumer<T> consumer() {
                    return consumer;
                }
            };
        }

        /**
         * 
         * @Title: produce
         * @Description: 生产线程结果，注意此时线程尚未调用
         * @author ZhangJianyang
         * @return
         */
        public PoolResult<T> produce() {
            return PoolResult.custom(scInfo).build();
        }

    }
}
